﻿using Roton.ZZT;

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Roton.Video
{
    /// <summary>
    /// A 2D grid of AnsiChars.
    /// </summary>
    public class VideoMatrix : IVideo
    {
        private int area;
        private int height;
        private AnsiChar[] matrix;
        private int width;

        public VideoMatrix(int width, int height)
        {
            this.width = width;
            this.height = height;
            this.area = width * height;
            this.matrix = new AnsiChar[area];
        }

        public AnsiChar GetChar(int x, int y)
        {
            return matrix[x + (y * width)];
        }

        public void PrintString(int x, int y, ByteString value, int color)
        {
            int index = x + (y * width);
            int length = value.Length;
            byte colorByte = (byte)(color & 0xFF);

            if (index >= 0)
                for (int i = 0; i < length && index < area; i++)
                    SetCharInternal(index++, new AnsiChar(value[i], colorByte));
        }

        public void SetChar(int x, int y, AnsiChar ac)
        {
            SetCharInternal(x + (y * width), ac);
        }

        // This function is designed to be overridden, in case further work needs
        // to be done (ex: screen updates)
        virtual protected void SetCharInternal(int index, AnsiChar ac)
        {
            matrix[index] = ac;
        }
    }
}
